package com.example.fw;

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;


import com.example.tests.ContactData;
import com.example.utils.SortedListOf;


public class ContactHelper extends WebDriverHelperBase{
	
	public static boolean CREATION = true;
	public static boolean MODIFICATION = false;

	public ContactHelper(ApplicationManager manager) {
		super(manager);
	}

	public ContactHelper createContact(ContactData contact) {
		manager.navigateTo().mainPage();
		initContactCreation();
		fillContactForm(contact, CREATION);
		submitContactCreation();
		returnToHomePage();
		manager.getModel().addContact(contact);
		return this;
	}
	
	public ContactHelper modifyContact(int index, ContactData contact) {
		manager.navigateTo().mainPage();
		initContactModification(index);
		fillContactForm(contact, MODIFICATION);
		submitContactModification();
		returnToHomePage();
		manager.getModel().removeContact(index);
		manager.getModel().addContact(contact);
		return this;
	}
	
	public ContactHelper deleteContact(int index) {
		selectForModifyContactByIndex(index);
		submitContactDeletion();
		returnToHomePage();
		manager.getModel().removeContact(index);
		return this;
	}
	
//	-----------------------------------------------------------------------------------------------------------------

	public ContactHelper initContactCreation() {
		click(By.linkText("add new"));
		return this;
	}

	public ContactHelper fillContactForm(ContactData contact, boolean formType) {
		type(By.name("firstname"), contact.getContactname());
		type(By.name("lastname"), contact.getLastname());		
		type(By.name("address"), contact.getAddress());		
		type(By.name("home"), contact.getHomePhone());
		type(By.name("mobile"), contact.getMobilePhone());
		type(By.name("work"), contact.getWorkPhone());
		type(By.name("email"), contact.getEmail());
		type(By.name("email2"), contact.getEmail2());
		if ((contact.getBday() != null) && (contact.getBmonth() != null)) {
			if (!(contact.getBday().equals("") && contact.getBmonth().equals(""))) {
				selectByText(By.name("bday"), contact.getBday());
				selectByText(By.name("bmonth"), driver.findElement(By.xpath("//select[2]/option[" + contact.getBmonth() + "]")).getText());
			}
		}
		type(By.name("byear"), contact.getByear());
		if(formType == CREATION){
//			selectByText(By.name("new_group"), "group 1");
		} else {
			if (driver.findElements(By.name("new_group")).size() != 0){
				throw new Error("Group selector exists in contact modification form");
			}
		}
		type(By.name("address2"), contact.getAddress2());
		type(By.name("phone2"), contact.getHomePhone2());
		return this;
	}

	public ContactHelper submitContactCreation() {
		click(By.name("submit"));
		return this;
	}

	public ContactHelper returnToHomePage() {
		click(By.linkText("home"));
		return this;
	}

	
	private void selectForModifyContactByIndex(int index) {
		click(By.xpath("//tr[@name='entry'][" + (index+1) + "]/td[7]/a/img"));
	}
	
	public ContactHelper initContactModification(int index) {
		selectForModifyContactByIndex(index);
		return this;
	}

	public ContactHelper submitContactModification() {
		click(By.xpath("//form/input[11]"));
		return this;
	}
	
	public void submitContactDeletion() {
		click(By.xpath("//form[2]/input[2]"));
	}
	
	private List<WebElement> getContactRows() {
		return driver.findElements(By.xpath("//tr[@name='entry']"));
	}

	private String getLastNameInRow(WebElement row) {
		    return row.findElement(By.xpath("./td[2]")).getText();
	}


	public SortedListOf<ContactData> getUIContacts() {
		SortedListOf<ContactData> contacts = new SortedListOf<ContactData>();
		
		manager.navigateTo().mainPage();
		List<WebElement> rows = getContactRows();
		for (WebElement row : rows) {
			String lastname =  getLastNameInRow(row);
			contacts.add(new ContactData().withLastname(lastname));
		}
		
		return contacts;
	}
}
